package com.by.chapter01;


import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class CheckBoxActivity extends AppCompatActivity implements
        CompoundButton.OnCheckedChangeListener, View.OnClickListener,RadioGroup.OnCheckedChangeListener{
    private TextView hobby;
    private String hobbys;
    private EditText name;
    private Button commit;
    private RadioGroup xl;
    CheckBox pingpong;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_box);
        //初始化CheckBox控件
        CheckBox shuttlecock = findViewById(R.id.like_shuttlecock);
        CheckBox basketball = findViewById(R.id.like_basketball);
        pingpong = findViewById(R.id.like_pingpong);
        xl = findViewById(R.id.xl);
        xl.setOnCheckedChangeListener(this);
        shuttlecock.setOnCheckedChangeListener(this);
        basketball.setOnCheckedChangeListener(this);
        pingpong.setOnCheckedChangeListener(this);
        commit = findViewById(R.id.commit);
        name = findViewById(R.id.name);
        commit.setOnClickListener(this);
        hobby = findViewById(R.id.hobby);
        hobbys = new String();//存放选中的CheckBox的文本信息
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        String motion = buttonView.getText().toString();
        if (isChecked) {
            if (!hobbys.contains(motion)) {
                hobbys = hobbys + motion;
                hobby.setText(hobbys);

            }
        } else {
            if (hobbys.contains(motion)) {
                hobbys = hobbys.replace(motion, "");
                hobby.setText(hobbys);
            }
        }
    }

    @Override
    public void onClick(View v) {
        //1 findViewById()
        //2 getText()
        Toast.makeText(this,name.getText(),Toast.LENGTH_SHORT).show();
        TextView tv = findViewById(R.id.inputName);
        tv.setText(name.getText());
        ImageView iv = findViewById(R.id.icon);
        iv.setImageResource(R.drawable.four);
        if(pingpong.isChecked()){
            Log.d("CheckBoxActivity","乒乓球");
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        Toast.makeText(this,checkedId+"",Toast.LENGTH_SHORT).show();
        RadioButton rb = (RadioButton) group.getChildAt(checkedId-1);
        if(checkedId==1){
            //研究生
        }
        Log.d("CheckBoxActivity",rb.getText().toString());

    }
}
